const { Sequelize } = require('sequelize');
const sequelize = new Sequelize(
  'graduation_recruitment', // 数据库名
  'root',                   // 用户名
  '511zb',                  // 密码
  {
    host: 'localhost',
    port: 3306,
    dialect: 'mysql',
    logging: false
  }
);
const Job = require('../models/Job');
const Resume = require('../models/Resume');
const Company = require('../models/Company');
const User = require('../models/User');


// 测试数据
const testCompanies = [
  {
    id: 'b1a7c8e0-1234-4cde-8f1a-123456789abc',
    name: '科技有限公司',
    description: '专注于软件开发的高科技企业',
    industry: '互联网',
    status: 'pending',
    createdAt: new Date(),
    updatedAt: new Date()
  },
  {
    id: 'b1a7c8e0-1234-4cde-8f1a-123456789abd',
    name: '金融服务集团',
    description: '提供全方位金融解决方案',
    industry: '金融',
    status: 'pending',
    createdAt: new Date(),
    updatedAt: new Date()
  }
];

const testJobs = [
  {
    title: '前端开发工程师',
    industry: '互联网',
    location: '北京',
    salary_range: '15k-25k',
    experience: '1-3年',
    education: '本科',
    description: '负责Web前端开发',
    requirements: '熟悉HTML/CSS/JavaScript',
    benefits: '五险一金，带薪年假',
    status: 'active',
    company_id: 'b1a7c8e0-1234-4cde-8f1a-123456789abc',
    created_at: new Date(),
    updated_at: new Date()
  },
  {
    title: '后端开发工程师',
    industry: '互联网',
    location: '上海',
    salary_range: '20k-30k',
    experience: '3-5年',
    education: '本科',
    description: '负责Node.js后端开发',
    requirements: '熟悉Node.js/Express',
    benefits: '弹性工作，年度体检',
    status: 'active',
    company_id: 'b1a7c8e0-1234-4cde-8f1a-123456789abd',
    created_at: new Date(),
    updated_at: new Date()
  }
  // ... 可继续添加
];

const testResumes = [
  {
    user_id: 1,
    full_name: '张三',
    gender: 'male',
    birth_date: '1998-01-01',
    phone: '13800000001',
    email: 'zhangsan1@example.com',
    highest_education: '本科',
    major: '计算机科学',
    school: '北京大学',
    graduation_date: '2020-07-01',
    work_experience: '3年Web开发经验',
    skills: 'JavaScript,React,Node.js',
    projects: '校园管理系统开发',
    self_introduction: '热爱编程，善于团队协作',
    status: 1,
    created_at: new Date(),
    updated_at: new Date()
  },
  {
    user_id: 2,
    full_name: '李四',
    gender: 'female',
    birth_date: '1995-05-15',
    phone: '13800000002',
    email: 'lisi2@example.com',
    highest_education: '硕士',
    major: '软件工程',
    school: '清华大学',
    graduation_date: '2019-07-01',
    work_experience: '5年Java开发经验',
    skills: 'Java,Spring,MySQL',
    projects: '电商平台开发',
    self_introduction: '责任心强，学习能力强',
    status: 1,
    created_at: new Date(),
    updated_at: new Date()
  }
  // ... 可继续添加
];

// 批量生成
const uuidList = [
  'b1a7c8e0-1234-4cde-8f1a-123456789abc',
  'b1a7c8e0-1234-4cde-8f1a-123456789abd'
];
const extraJobs = Array.from({ length: 100 }, (_, i) => ({
  title: randomFrom(jobTitles) + (i+1),
  industry: randomFrom(industries),
  location: randomFrom(locations),
  salary_range: randomFrom(salaryRanges),
  experience: randomFrom(experiences),
  education: randomFrom(educations),
  description: '这是职位描述示例' + (i+1),
  requirements: '这是职位要求示例' + (i+1),
  benefits: '五险一金',
  status: 'active',
  company_id: randomFrom(uuidList),
  created_at: new Date(),
  updated_at: new Date()
}));

// 批量生成
const genders = ['male', 'female', 'other'];
const extraResumes = Array.from({ length: 100 }, (_, i) => ({
  user_id: getRandomInt(1, 20),
  full_name: '测试用户' + (i+1),
  gender: randomFrom(genders),
  birth_date: `199${getRandomInt(0,9)}-0${getRandomInt(1,9)}-${getRandomInt(10,28)}`,
  phone: '138' + String(10000000 + i).padStart(8, '0'),
  email: `testuser${i+1}@example.com`,
  highest_education: randomFrom(educations),
  major: randomFrom(majors),
  school: randomFrom(schools),
  graduation_date: `20${getRandomInt(10,22)}-07-01`,
  work_experience: `${getRandomInt(1,10)}年相关经验`,
  skills: randomFrom(skillsArr),
  projects: '项目经验示例' + (i+1),
  self_introduction: '自我介绍示例' + (i+1),
  status: 1,
  created_at: new Date(),
  updated_at: new Date()
}));

// 生成更多职位和简历样例数据
function getRandomInt(min, max) {
  return Math.floor(Math.random() * (max - min + 1)) + min;
}
function randomFrom(arr) { return arr[getRandomInt(0, arr.length - 1)]; }

const industries = ['互联网', '金融', '医疗', '教育', '制造业', '服务业'];
const locations = ['北京', '上海', '广州', '深圳', '杭州', '成都'];
const jobTitles = ['前端开发工程师', '后端开发工程师', '产品经理', '数据分析师', 'UI设计师', '测试工程师', '运维工程师', '市场专员'];
const salaryRanges = ['10k-15k', '15k-25k', '20k-30k', '25k-40k', '18k-28k', '12k-18k', '16k-26k', '15k-22k'];
const experiences = ['0-2年', '1-3年', '2-4年', '3-5年', '5-7年'];
const educations = ['大专', '本科', '硕士'];
const jobTypes = ['全职', '兼职', '实习'];

const majors = ['计算机科学', '软件工程', '数据科学', '电子信息', '通信工程', '自动化', '信息管理'];
const schools = ['北京大学', '清华大学', '复旦大学', '上海交通大学', '中山大学', '浙江大学', '华中科技大学', '哈尔滨工业大学'];
const skillsArr = ['JavaScript,React,Node.js', 'Java,Spring,MySQL', 'Python,数据分析,机器学习', 'Vue,ElementUI,Webpack', 'PHP,MySQL,Laravel', 'UI设计,Photoshop,Figma', '网络安全,Linux,Python', 'C++,嵌入式开发'];


// 批量创建测试数据
async function seedData() {
  try {
    // 连接数据库
    await sequelize.authenticate();
    console.log('数据库连接成功');

    // 使用事务确保数据一致性
    await sequelize.transaction(async (t) => {
      // 创建公司数据
      await Company.bulkCreate(testCompanies, { transaction: t });
      console.log('公司测试数据创建成功');

      // 创建职位数据
      await Job.bulkCreate(testJobs.concat(extraJobs), { transaction: t });
      console.log('职位测试数据创建成功');

      // 创建简历数据
      await Resume.bulkCreate(testResumes.concat(extraResumes), { transaction: t });
      console.log('简历测试数据创建成功');
    });

    console.log('所有测试数据创建成功');
  } catch (error) {
    console.error('测试数据创建失败:', error);
  } finally {
    // 关闭数据库连接
    await sequelize.close();
  }
}

// 执行数据填充
seedData();